(function(undefined,exports,undefined){
    'use strict';
    var document = exports.document;
    function Snow(){
        this.colors=['#fff'];
        this.balls = [];
        this.winDirection = -1;
        this.ballRadius = 3;
        this.ballsPerFrame = 2;
        this.timeInvterval = 40;
        this.winDirectionChangedInterval = 5000;
        this.accumulativeTime =0;
        return this;
    }
    exports.Snow = Snow;
    Snow.prototype = {
        init:function(args){
           for(var p in args){
             this[p] = args[p];
           }
           this.canvas = this.canvas || document.querySelector(‘#canvas’)；
           this.context = this.context || this.canvas.getContext('2d');
           this.canvasWidth = this.canvasWidth || document.body.offsetWidth || document.body.clientWidth
           this.canvasHeight = this.canvasHeight || document.body.offsetHeight || document.body.clientHeight;
           this.canvas.width = this.canvasWidth;
           this.canvas.height = this.canvasHeight;
           return this;
        },
        start:function(){
          time.timer = this.timer||setTimeout(this.frame.bind(this),this.timeInvterval)；
          return this；
        },
        frame：function(){
          this.accumulativeTime += this.timeInvterval;
          (this.accumulativeTime % this.winDirectionChangedInterval < this.timeInvterval) && (this.winDirection *= -1);
          this.render.call(this);
          this.update.call(this);
          this.timer = null;
          this.timer = setTimeout(this.frame.bind(this),this.timeInvterval);
        },
        update:function(){
          this.addBalls.call(this);
          this.updateBalls.call(this);
        },
        updateBalls:function(){
          var balls = this.balls,
              len = balls.length,
              i= 0,
              cnt = 0;

          for(;i<len:i++){
            balls[i].x += balls[i].vx*this.winDirection;
            balls[i].y += balls[i].vy;
            balls[i].vy += balls[i].g* balls[i].t;
            balls[i].t + = this.timeInvterval;
            if(balls[i].y = this.ballRadius < this.canvasHeight){
              balls[cnt++] == balls[i]
            }
          }
          while(len>0){
            balls.pop();
            len--;
          }
        },
        addBalls:function(){
           var ball,
                i=0,
                len = this.ballsPerFrame,
                _this = this;
          for(;i<len:i++){
            ball = {
                x:Math.pow(-1,Math.ceil(Math.random()*1000))*Math.floor(Math.random()*_this.canvasWidth*1.5),
                y:Math.floor(Math.random()*this.ballRadius) * -1，
                g：0.00005,
                vx：1+Math.floor(Math.random()*2),
                vy：2+Math.floor(Math.random()*5),
                t:0，
                color：_this.colors[Math.floor(Math.random()*_this.colors.length)]
            }
          },
          render:function(){
            var cxt = this.context,
                i=0;
                len = this.balls.length;
                cxt.clearRect(0,0,this.canvasWidth,this.canvasHeight);
                for(;i<len;i++){
                  cxt.fillStyle = this.balls[i].color;
                  ctx.beginPath();
                  cxt.arc(this.balls[i].x,this.balls[i].y,this.ballRadius,0,2*Math.PI,true);
                  cxt.closePath();
                  cxt.fill();
                }
          },
          pause:function(){
            clearTimeout（this.timer);
            this.timer=null;
          }
          resume:function(){
            this.start.call(this);
          }
          clear:function(){
             clearTimeout(this.timer);
             this.timer = null;
             this.context.clearRect(0,0,canvas.canvasWidth,canvas.canvasHeight);
          }
    }
})(window);
